﻿Imports System
Imports System.IO.Ports
Imports System.Threading

Public Class Form2
    Dim Level As Integer
    Dim Bonus As Integer
    Dim Score As Integer
    Dim Moves As Integer
    Dim Time As Integer
    Dim LogicMap(0 To 4, 0 To 4) As Integer
    Dim Map(0 To 4, 0 To 4) As PictureBox
    Dim Random(0 To 124) As Integer
    Dim newnumber As Integer
    Dim GamePadButton As String
    Dim x9, y9 As Integer

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        LabelNname.Text = Form1.PlayerName
        Bonus = 100
        ProgressBar1.Value = Bonus
        Select Case Form1.Character
            Case 1
                PictureBox1.Image = My.Resources.stan
            Case 2
                PictureBox1.Image = My.Resources.kyle
            Case 3
                PictureBox1.Image = My.Resources.kenny
            Case 4
                PictureBox1.Image = My.Resources.butters
            Case 5
                PictureBox1.Image = My.Resources.robert
        End Select
        Level = 0
        gamepadconection()
        StartPuzzle()
    End Sub

    Private Sub StartPuzzle()
        Timer1.Interval = 1000
        InitMatrix()
        fillmap()
        Timer1.Enabled = True
        Timer2.Enabled = True

    End Sub


    Private Function InitMatrix() As Boolean
        Dim x, y As Integer
        Dim check As Boolean


        Select Case Level
            Case 0 To 3
                newnumber = 0
                For y = 0 To 2
                    For x = 0 To 2
                        Do While check <> True
                            LogicMap(y, x) = CInt(Int((9 * Rnd()) + 1))
                            check = checkmap(LogicMap(y, x))
                        Loop
                        Random(newnumber) = LogicMap(y, x)
                        newnumber += 1
                        check = False
                    Next
                Next
        End Select
        Return True
    End Function

    Private Function checkmap(ByVal number As Integer) As Boolean
        Dim pos As Integer

        For pos = 0 To newnumber
            If number = Random(pos) Then
                Return False
            End If
        Next
        Return True
    End Function

    Private Function fillmap() As Boolean
        Dim x, y, xpos, ypos As Integer
        xpos = 540 / 3
        ypos = 520 / 3

        For y = 0 To 2
            For x = 0 To 2
                Map(y, x) = New PictureBox()
                Me.Controls.Add(Map(y, x))
                Map(y, x).Left = 20 + (xpos * x)
                Map(y, x).Top = 20 + (ypos * y)
                Map(y, x).Width = xpos
                Map(y, x).Height = ypos
                Map(y, x).SizeMode = PictureBoxSizeMode.StretchImage
                Select Case LogicMap(y, x)
                    Case 1
                        Map(y, x).Image = My.Resources.level_0_1
                    Case 2
                        Map(y, x).Image = My.Resources.level_0_2
                    Case 3
                        Map(y, x).Image = My.Resources.level_0_3
                    Case 4
                        Map(y, x).Image = My.Resources.level_0_4
                    Case 5
                        Map(y, x).Image = My.Resources.level_0_5
                    Case 6
                        Map(y, x).Image = My.Resources.level_0_6
                    Case 7
                        Map(y, x).Image = My.Resources.level_0_7
                    Case 8
                        Map(y, x).Image = My.Resources.level_0_8
                    Case 9
                        x9 = x
                        y9 = y
                End Select
            Next
        Next
        Return True
    End Function

    Private Function CheckImage() As Boolean
        Dim x, y, counter, answer As Integer
        answer = 1
        For y = 0 To 2
            For x = 0 To 2
                If LogicMap(y, x) = answer Then
                    counter += 1
                End If
                answer += 1
            Next
        Next
        If counter = 8 Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Function move_image(ByVal dir As Integer) As Boolean
        Dim tmp As Integer

        Select Case dir

            Case 0 'move down
                If y9 <> 0 Then
                    tmp = LogicMap(y9 - 1, x9)
                    LogicMap(y9 - 1, x9) = 9
                    LogicMap(y9, x9) = tmp
                End If
            Case 1 'move up
                If y9 <> 2 Then
                    tmp = LogicMap(y9 + 1, x9)
                    LogicMap(y9 + 1, x9) = 9
                    LogicMap(y9, x9) = tmp
                End If
            Case 2 'move to the left
                If x9 <> 0 Then
                    tmp = LogicMap(y9, x9 - 1)
                    LogicMap(y9, x9 - 1) = 9
                    LogicMap(y9, x9) = tmp
                End If
            Case 3 'move to the right
                If x9 <> 2 Then
                    tmp = LogicMap(y9, x9 + 1)
                    LogicMap(y9, x9 + 1) = 9
                    LogicMap(y9, x9) = tmp
                End If
        End Select
        Return True
    End Function

    
    Private Function refillmap()
        Dim x, y As Integer


        For y = 0 To 2
            For x = 0 To 2
                Select Case LogicMap(y, x)
                    Case 1
                        Map(y, x).Image = My.Resources.level_0_1
                    Case 2
                        Map(y, x).Image = My.Resources.level_0_2
                    Case 3
                        Map(y, x).Image = My.Resources.level_0_3
                    Case 4
                        Map(y, x).Image = My.Resources.level_0_4
                    Case 5
                        Map(y, x).Image = My.Resources.level_0_5
                    Case 6
                        Map(y, x).Image = My.Resources.level_0_6
                    Case 7
                        Map(y, x).Image = My.Resources.level_0_7
                    Case 8
                        Map(y, x).Image = My.Resources.level_0_8
                    Case 9
                        Map(y, x).Image = Nothing
                        x9 = x
                        y9 = y
                        
                End Select
            Next
        Next
        Return True
    End Function

    Private Function finished()
        Score = 10000 / (Time * Moves)
        If (Bonus <> 0) Then
            Score *= Bonus
            LabelScore.Text = Score
            Level += 1
            LabelLevel.Text = Level
        End If
    End Function

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Time += 1
        If (Bonus) Then
            Bonus -= 1
            ProgressBar1.Value = Bonus
        End If
    End Sub


    Private Function gamepadconection()
        If Not (Form1.PadEvents.IsOpen) Then
            Form1.PadEvents.PortName = Form1.Portname
            Form1.PadEvents.Open()
        End If
        '.DataReceived
        Return True
    End Function

    Private Function gamepadread() As String
        Dim Data As String
        Dim cad(0 To 10) As Byte
        cad = Form3.ReadPad(1)
        Data = System.Text.Encoding.Default.GetString(cad)
        cad.DefaultIfEmpty()
        Return Data.First
    End Function


    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim datain As String
        ' Form1.PadEvents.Open()
        Timer2.Stop()
        Form1.PadEvents.DiscardInBuffer()
        datain = gamepadread()
        'MsgBox(datain)
        Select Case datain
            Case "U"
                move_image(1)
            Case "D"
                move_image(0)
            Case "L"
                move_image(3)
            Case "R"
                move_image(2)
        End Select
        refillmap()
        If (CheckImage()) Then
            finished()
        End If
        Timer2.Start()
    End Sub
End Class